package org.hank.compose_yyzs.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.orhanobut.logger.Logger
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import org.hank.compose_yyzs.R
import org.hank.compose_yyzs.bean.MerchantInfo
import org.hank.compose_yyzs.bean.WifiBean
import org.hank.compose_yyzs.db.dao.WifiDao
import org.hank.compose_yyzs.utils.getStringGlobal
import javax.inject.Inject

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/9/4 16:43
 *描述:
 */
@HiltViewModel
class WifiSetViewModel @Inject constructor(
    var wifiDao: WifiDao
) : ViewModel() {
    var wifiName = MutableStateFlow("")
    var wifiNameFocus = MutableStateFlow(false)
    var wifiPass = MutableStateFlow("")
    var wifiPassFocus = MutableStateFlow(false)
    var wifiIp1 = MutableStateFlow("")
    var wifiIp2 = MutableStateFlow("")
    var wifiIp3 = MutableStateFlow("")
    var wifiIp4 = MutableStateFlow("")

    private val _viewEvents = Channel<WifiSettingEvent>(Channel.BUFFERED)
    val viewEvents = _viewEvents.receiveAsFlow()

    suspend fun saveWifiInfo(): Deferred<String> {
        return viewModelScope.async {
            if (wifiName.value.isEmpty()) {
                _viewEvents.send(WifiSettingEvent.ErrorMessage(getStringGlobal(R.string.setting_wifi_name_hint)))
                return@async ""
            }
            if (wifiPass.value.isEmpty()) {
                _viewEvents.send(WifiSettingEvent.ErrorMessage(getStringGlobal(R.string.setting_wifi_pass_hint)))
                return@async ""
            }
            if (wifiIp1.value.isEmpty() ||
                wifiIp2.value.isEmpty() ||
                wifiIp3.value.isEmpty() ||
                wifiIp4.value.isEmpty()
            ) {
                _viewEvents.send(WifiSettingEvent.ErrorMessage(getStringGlobal(R.string.setting_wifi_address_empty)))
                return@async ""
            }

            var wifiBean = WifiBean(
                wifiName = wifiName.value,
                wifiPass = wifiPass.value,
                wifiAddress = "${wifiIp1.value}.${wifiIp2.value}.${wifiIp3.value}.${wifiIp4.value}",
                isCurrentWifi = true
            )
            var isHistory =
                wifiDao.isHistoryWifi(wifiBean.wifiName, wifiBean.wifiAddress)
            wifiDao.updateAllToFalse()
            if (isHistory) {
                wifiBean.id = wifiDao.getIdByWifiName(wifiName = wifiBean.wifiName)
                wifiDao.editWifiInfo(wifiBean)
            } else {
                wifiDao.insertWifiInfo(wifiBean)
            }
            _viewEvents.send(WifiSettingEvent.ErrorMessage(getStringGlobal(R.string.setting_wifi_config_ret)))
            "wifi Save Finish"
        }
    }

    fun resetUiState(){
         wifiName = MutableStateFlow("")
         wifiNameFocus = MutableStateFlow(false)
         wifiPass = MutableStateFlow("")
         wifiPassFocus = MutableStateFlow(false)
         wifiIp1 = MutableStateFlow("")
         wifiIp2 = MutableStateFlow("")
         wifiIp3 = MutableStateFlow("")
         wifiIp4 = MutableStateFlow("")
    }
}

sealed class WifiSettingEvent {
    data class ErrorMessage(val message: String) : WifiSettingEvent()
}